package bb.lanxing.fragment.base;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.PopupWindow;

import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.common.messager.EventMessageListener;
import bb.lanxing.mvp.base.IViewInterface;
import bb.lanxing.mvvm.base.IPermissionInspector;
import bb.lanxing.util.DialogUtil;
import bb.lanxing.util.Log;

public abstract class BaseFragment extends Fragment implements IViewInterface, EventMessageListener, IPermissionInspector {
    private static final String TAG = "fragmentLife";
    private IViewInterface delegate;
    private Handler handler;
    private DialogUtil.LoadingPopupWindow mProgressPopWindow;

    public boolean onBackPressed() {
        return false;
    }

    public boolean onMessage(int i, int i2, int i3, Object obj, Bundle bundle) {
        return false;
    }

    protected boolean onPermissionDenied(String[] strArr) {
        return false;
    }

    protected void onPermissionGranted() {
    }

    protected boolean registerMessageEvent() {
        return false;
    }

    public BaseFragment() {
        this.mProgressPopWindow = null;
        this.handler = new Handler();
    }

    public BaseFragment(int i) {
        super(i);
        this.mProgressPopWindow = null;
        this.handler = new Handler();
    }

    @Override
    public void onAttach(Context context) {
        Log.d(TAG, "onAttach " + getClass().getSimpleName() + " tag = " + getTag());
        super.onAttach(context);
        if (registerMessageEvent()) {
            App.getContext().getMessageSender().registerListener(this);
        }
        if (context instanceof IViewInterface) {
            this.delegate = (IViewInterface) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (registerMessageEvent()) {
            App.getContext().getMessageSender().unregisterListener(this);
        }
        this.delegate = null;
    }

    @Override
    public void onDestroyView() {
        Log.d(TAG, "onDestroyView " + getClass().getSimpleName() + " tag = " + getTag());
        super.onDestroyView();
    }

    @Override
    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        Log.d(TAG, "onActivityCreated " + getClass().getSimpleName() + " tag = " + getTag());
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName());
        Log.d(TAG, "onResume " + getClass().getSimpleName() + " tag = " + getTag());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
        Log.d(TAG, "onPause " + getClass().getSimpleName() + " tag = " + getTag());
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop " + getClass().getSimpleName() + " tag = " + getTag());
    }

    protected boolean darkStatusBarIcon(boolean z) {
        BaseActivity baseActivity = (BaseActivity) getActivity();
        return baseActivity != null && baseActivity.darkStatusBarIcon(z);
    }

    @Override
    public void setUserVisibleHint(boolean z) {
        super.setUserVisibleHint(z);
        Log.d(TAG, "setUserVisibleHint ====== " + getClass().getSimpleName() + " ========== " + z);
    }

    @Override
    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        Log.d(TAG, "onSaveInstanceState " + getClass().getSimpleName());
    }

    @Override
    public void onViewStateRestored(Bundle bundle) {
        super.onViewStateRestored(bundle);
        Log.d(TAG, "onViewStateRestored " + getClass().getSimpleName());
    }

    @Deprecated
    public void showMyProgressDialog() {
        showLoadingDialog(R.string.dialog_loading, true);
    }

    @Deprecated
    public void showMyProgressDialog(int i) {
        showLoadingDialog(i, true);
    }

    @Override
    public void showLoadingDialog(int i, boolean z) {
        showLoadingDialog(getString(i), z, null);
    }

    @Override
    public void showLoadingDialog(CharSequence charSequence, boolean z) {
        showLoadingDialog(charSequence, z, null);
    }

    @Override
    public void showLoadingDialog(int i, DialogInterface.OnCancelListener onCancelListener) {
        showLoadingDialog(getString(i), true, onCancelListener);
    }

    @Override
    public void showLoadingDialog(CharSequence charSequence, DialogInterface.OnCancelListener onCancelListener) {
        showLoadingDialog(charSequence, true, onCancelListener);
    }

    @Override
    public void showLoadingDialog(CharSequence charSequence, boolean z, DialogInterface.OnCancelListener onCancelListener) {
        IViewInterface iViewInterface = this.delegate;
        if (iViewInterface != null) {
            iViewInterface.showLoadingDialog(charSequence, z, onCancelListener);
        }
    }

    @Override
    public void showLoadingDialog(int i, boolean z, DialogInterface.OnCancelListener onCancelListener) {
        IViewInterface iViewInterface = this.delegate;
        if (iViewInterface != null) {
            iViewInterface.showLoadingDialog(i, z, onCancelListener);
        }
    }

    @Override
    public void dismissLoadingDialog() {
        IViewInterface iViewInterface = this.delegate;
        if (iViewInterface != null) {
            iViewInterface.dismissLoadingDialog();
        }
    }

    @Override
    public void snack(CharSequence charSequence) {
        if (this.delegate == null || getView() == null) {
            return;
        }
        Snackbar.make(getView(), charSequence, -1).setTextColor(-1).show();
    }

    @Override
    public void snack(int i) {
        if (this.delegate == null || getView() == null) {
            return;
        }
        Snackbar.make(getView(), i, -1).setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE).setTextColor(-1).show();
    }

    @Override
    public void snackLong(CharSequence charSequence) {
        if (this.delegate == null || getView() == null) {
            return;
        }
        Snackbar.make(getView(), charSequence, 0).setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE).setTextColor(-1).show();
    }

    @Override
    public void snackLong(int i) {
        if (this.delegate == null || getView() == null) {
            return;
        }
        Snackbar.make(getView(), i, 0).setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE).setTextColor(-1).show();
    }

    @Override
    public void snack(CharSequence charSequence, boolean z) {
        if (this.delegate == null || getView() == null) {
            return;
        }
        Snackbar actionTextColor = Snackbar.make(getView(), charSequence, -1).setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE).setTextColor(-1).setActionTextColor(-1);
        actionTextColor.getView().setBackgroundColor(getResources().getColor(z ? R.color.snack_succes : R.color.common_red));
        actionTextColor.show();
    }

    @Override
    public void snack(int i, boolean z) {
        if (this.delegate == null || getView() == null) {
            return;
        }
        snack(getResources().getString(i), z);
    }

    @Override
    public void snackLong(CharSequence charSequence, boolean z) {
        if (this.delegate == null || getView() == null) {
            return;
        }
        Snackbar.make(getView(), charSequence, 0).setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE).setTextColor(-1).show();
    }

    @Override
    public void snackLong(int i, boolean z) {
        if (this.delegate == null || getView() == null) {
            return;
        }
        snackLong(getResources().getString(i), z);
    }

    @Override
    public void snackTop(CharSequence charSequence) {
        if (this.delegate == null || getView() == null) {
            return;
        }
//        TopSnackBar.Companion.makeText(getView(), charSequence, TopSnackBar.Companion.getLENGTH_SHORT()).showWithAnimate();
    }

    @Override
    public void snackTop(int i) {
        if (this.delegate == null || getView() == null) {
            return;
        }
//        TopSnackBar.Companion.makeText(getView(), i, TopSnackBar.Companion.getLENGTH_SHORT()).showWithAnimate();
    }

    @Override
    public void snackTopLong(CharSequence charSequence) {
        if (this.delegate == null || getView() == null) {
            return;
        }
//        TopSnackBar.Companion.makeText(getView(), charSequence, TopSnackBar.Companion.getLENGTH_LONG()).showWithAnimate();
    }

    @Override
    public void snackTopLong(int i) {
        if (this.delegate == null || getView() == null) {
            return;
        }
//        TopSnackBar.Companion.makeText(getView(), i, TopSnackBar.Companion.getLENGTH_LONG()).showWithAnimate();
    }

    @Override
    public void snackTop(CharSequence charSequence, int i, int i2) {
        if (this.delegate == null || getView() == null) {
            return;
        }
//        TopSnackBar.Companion.makeText(getView(), charSequence, TopSnackBar.Companion.getLENGTH_SHORT(), i, i2).showWithAnimate();
    }

    @Override
    public void snackTop(int i, int i2, int i3) {
        if (this.delegate == null || getView() == null) {
            return;
        }
//        TopSnackBar.Companion.makeText(getView(), i, TopSnackBar.Companion.getLENGTH_SHORT(), i2, i3).showWithAnimate();
    }

    @Override
    public void snackTopLong(CharSequence charSequence, int i, int i2) {
        if (this.delegate == null || getView() == null) {
            return;
        }
//        TopSnackBar.Companion.makeText(getView(), charSequence, TopSnackBar.Companion.getLENGTH_LONG(), i, i2).showWithAnimate();
    }

    @Override
    public void snackTopLong(int i, int i2, int i3) {
        if (this.delegate == null || getView() == null) {
            return;
        }
//        TopSnackBar.Companion.makeText(getView(), i, TopSnackBar.Companion.getLENGTH_LONG(), i2, i3).showWithAnimate();
    }

    public void initLoadingPopWindow() {
        if (this.mProgressPopWindow == null) {
            this.mProgressPopWindow = DialogUtil.initProgressDialog(getActivity(), new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    BaseFragment.this.closePopWindow();
                }
            });
        }
    }

    public void showLoadingPopWindow(String str) {
        initLoadingPopWindow();
        DialogUtil.showProgressDialog(getActivity(), this.mProgressPopWindow, str);
    }

    public void closePopWindow() {
        if (this.mProgressPopWindow != null) {
            this.handler.post(new Runnable() {
                @Override
                public void run() {
                    DialogUtil.hideDialog(BaseFragment.this.mProgressPopWindow);
                    BaseFragment.this.mProgressPopWindow = null;
                }
            });
        }
    }

    @Override
    public void dismissLoadingDialogWithDoneAnimation() {
        IViewInterface iViewInterface = this.delegate;
        if (iViewInterface != null) {
            iViewInterface.dismissLoadingDialogWithDoneAnimation();
        }
    }

    @Override
    public void toast(CharSequence charSequence) {
        IViewInterface iViewInterface = this.delegate;
        if (iViewInterface != null) {
            iViewInterface.toast(charSequence);
        }
    }

    @Override
    public void toast(int resId) {
        toast(App.getContext().getString(resId));
    }

    @Override
    public void toastLong(CharSequence charSequence) {
        IViewInterface iViewInterface = this.delegate;
        if (iViewInterface != null) {
            iViewInterface.toastLong(charSequence);
        }
    }

    @Override
    public void toastLong(int resId) {
        toastLong(App.getContext().getString(resId));
    }

    public void sendMessage(int what) {
        sendMessageDelayed(what, 0, 0, null, null, 0L);
    }

    public void sendMessage(int what, Bundle bundle) {
        sendMessageDelayed(what, 0, 0, null, bundle, 0L);
    }

    public void sendMessage(int what, Object obj) {
        sendMessageDelayed(what, 0, 0, obj, null, 0L);
    }

    public void sendMessage(int what, int arg1, int arg2) {
        sendMessageDelayed(what, arg1, arg2, null, null, 0L);
    }

    public void sendMessage(int what, int arg1, int arg2, Bundle bundle) {
        sendMessageDelayed(what, arg1, arg2, null, bundle, 0L);
    }

    public void sendMessage(int what, int arg1, int arg2, Object obj) {
        sendMessageDelayed(what, arg1, arg2, obj, null, 0L);
    }

    public void sendMessage(int what, int arg1, int arg2, Object obj, Bundle bundle) {
        sendMessageDelayed(what, arg1, arg2, obj, bundle, 0L);
    }

    public void sendMessageDelayed(int what, int arg1, int arg2, Object obj, Bundle bundle, long delayMillis) {
        App.getContext().getMessageSender().sendMessageDelayed(what, arg1, arg2, obj, bundle, delayMillis);
    }

    public void runOnUIThread(Runnable runnable) {
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            runnable.run();
        } else {
            App.getContext().getHandler().post(runnable);
        }
    }

    @Override
    public boolean checkAndRequestPermissions(String[] strArr, int i) {
        return checkAndRequestPermissions(strArr, i > 0 ? getString(i) : null);
    }

    @Override
    public boolean checkAndRequestPermissions(String[] strArr, CharSequence charSequence) {
        final String[] checkPermissions;
        if (strArr == null || strArr.length <= 0 || (checkPermissions = checkPermissions(strArr)) == null) {
            return true;
        }
        boolean z = false;
        for (String str : checkPermissions) {
            z |= shouldShowRequestPermissionRationale(str);
        }
        if (z && !TextUtils.isEmpty(charSequence)) {
            DialogUtil.adjustDialogGravityCenter(new AlertDialog.Builder(getContext()).setTitle(R.string.dialog_prompt).setMessage(charSequence).setPositiveButton(R.string.confirm, (dialogInterface, i) -> BaseFragment.this.requestPermissions(checkPermissions, 1)).setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
                if (!BaseFragment.this.onPermissionDenied(checkPermissions)) {
                    BaseFragment.this.toast(R.string.sport_all_permission_title);
                    if (BaseFragment.this.getActivity() == null) {
                        return;
                    }
                    BaseFragment.this.getActivity().finish();
                }
            }).setCancelable(false).show());
        } else {
            requestPermissions(checkPermissions, 1);
        }
        return false;
    }

    @Override
    public boolean checkAndRequestPermissionsWithRationale(String[] strArr, IPermissionInspector.PermissionRationaleCallback permissionRationaleCallback) {
        final String[] checkPermissions;
        if (strArr == null || strArr.length <= 0 || (checkPermissions = checkPermissions(strArr)) == null) {
            return true;
        }
        if (permissionRationaleCallback == null) {
            return false;
        }
        permissionRationaleCallback.onRequestRationale(new IPermissionInspector.PermissionRationaleConfirmCallback() {
            @Override
            public String[] getCurrentRequiredPermissions() {
                return checkPermissions;
            }

            @Override
            public void onRequestRationaleConfirmed() {
                BaseFragment.this.requestPermissions(checkPermissions, 1);
            }
        });
        return false;
    }

    private String[] checkPermissions(String[] strArr) {
        ArrayList arrayList = new ArrayList();
        for (String str : strArr) {
            if (ContextCompat.checkSelfPermission(getContext(), str) == -1) {
                arrayList.add(str);
            }
        }
        if (arrayList.isEmpty()) {
            return null;
        }
        return (String[]) arrayList.toArray(new String[arrayList.size()]);
    }

    @Override
    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        super.onRequestPermissionsResult(i, strArr, iArr);
        if (i == 1) {
            ArrayList arrayList = new ArrayList();
            for (int i2 = 0; i2 < iArr.length; i2++) {
                if (iArr[i2] == -1) {
                    arrayList.add(strArr[i2]);
                }
            }
            if (!arrayList.isEmpty()) {
                if (onPermissionDenied((String[]) arrayList.toArray(new String[arrayList.size()]))) {
                    return;
                }
                toast(R.string.sport_all_permission_title);
                return;
            }
            onPermissionGranted();
        }
    }
}